package com.xjoyt.pay.master.dao.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;

import com.xjoyt.pay.abstracts.AbstractDao;
import com.xjoyt.pay.dto.PageParam;
import com.xjoyt.pay.dto.PageResult;
import com.xjoyt.pay.master.dao.CashRepositoryCustom;
import com.xjoyt.pay.master.dao.predicate.CashCommands;
import com.xjoyt.pay.master.params.MerchantCashPageParam;
import com.xjoyt.pay.master.po.Cash;
import com.xjoyt.pay.master.repository.CashRepository;

public class CashRepositoryImpl extends AbstractDao implements CashRepositoryCustom {

	@Autowired
	private CashRepository repository;

	@Override
	public PageResult<Cash> loadPage(PageParam<MerchantCashPageParam> param) {
		if (param.query.startDate != null && param.query.endDate != null) {
			param.query.startDate = toStartDate(param.query.startDate);
			param.query.endDate = toEndDate(param.query.endDate);
		}
		Pageable pageable = getPageable(param);
		Specification<Cash> spec = getSpecification(CashCommands.class, param.query);
		Page<Cash> pageResult = repository.findAll(spec, pageable);
		return new PageResult<>(pageResult.getTotalElements(), pageResult.getContent());
	}

}
